import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from '../components/Login.vue'
const Login = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
// import Home from '../components/Home.vue'
const Home = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Home.vue')
// import Welcome from '../components/Welcome.vue'
const Welcome = () => import( /* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')
// import User from '../components/user/User.vue'
const User = () => import( /* webpackChunkName: "User_Right_Role" */ '../components/user/User.vue')
// import Role from '../components/power/Role.vue'
const Role = () => import( /* webpackChunkName: "User_Right_Role" */ '../components/power/Role.vue')
// import Right from '../components/power/Right.vue'
const Right = () => import( /* webpackChunkName: "User_Right_Role" */ '../components/power/Right.vue')
// import Cate from '../components/goods/Cate.vue'
const Cate = () => import( /* webpackChunkName: "Cate_Params" */ '../components/goods/Cate.vue')
// import Params from '../components/goods/Params.vue'
const Params = () => import( /* webpackChunkName: "Cate_Params" */ '../components/goods/Params.vue')
// import List from '../components/goods/List.vue'
const List = () => import( /* webpackChunkName: "List_AddGood" */ '../components/goods/List.vue')
// import AddGood from '../components/goods/AddGood.vue'
const AddGood = () => import( /* webpackChunkName: "List_AddGood" */ '../components/goods/AddGood.vue')
// import Order from '../components/order/Order.vue'
const Order = () => import( /* webpackChunkName: "Order_Report" */ '../components/order/Order.vue')
// import Report from '../components/report/Report.vue'
const Report = () => import( /* webpackChunkName: "Order_Report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        component: Welcome
      }, {
        path: '/users',
        component: User
      }, {
        path: '/roles',
        component: Role
      }, {
        path: '/rights',
        component: Right
      }, {
        path: '/goods',
        component: List
      }, {
        path: '/goods/add',
        component: AddGood
      }, {
        path: '/params',
        component: Params
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
});

// 配置路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()  放行    next('/login')  强制跳转
  // if (to.path == '/login') {
  //   next()
  // } else {
  //   let tokenStr = sessionStorage.getItem('token');
  //   if (tokenStr) {
  //     next()
  //   } else {
  //     next('/login')
  //   }
  // }
  // 代码优化
  // console.log(to);
  // 如果要访问login页面，则放行，return 终止后续代码执行
  if (to.path == '/login') return next();
  // 如果访问的不是login页面，则获取本地session中的token
  let tokenStr = sessionStorage.getItem('token');
  // 如果token不存在，表示用户未登录，则跳转登录页，return 终止后续代码执行
  if (!tokenStr) return next('/login');
  // 如果token存在，表明用户已登录，则放行
  next();
})

export default router